package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResActivity;
import com.fhnew.app.model.network.entity.req.ResBanner;
import com.fhnew.app.util.OnItemClickListener;
import com.fhnew.app.util.SYSOUtils;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动
 */
public class HuodongActivity extends Activity {

    ImageView close;
    ImageView huodongimgbt;
    ImageView gonggaoimgbt;
    ImageView activityimg;
    TextView huodongtext;
    TextView gonggao;
    TextView gonggaotext;
    RecyclerView activtylist;
    Adapter adapter;
    ArrayList<ResBanner.DataBean.NoticesBean> notices = new ArrayList<>();
    ArrayList<ResActivity.DataBean.QpActivityBean> activityBeans = new ArrayList<>();
    int flag;
    private int positions = 0;

    @Override
    protected void onPause() {
        super.onPause();
//        EventBus.getDefault().post(new ReloginEvent("100"));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huodong);
        close = findViewById(R.id.close);
        huodongimgbt = findViewById(R.id.huodongimgbt);
        gonggaoimgbt = findViewById(R.id.gonggaoimgbt);
        huodongtext = findViewById(R.id.huodongtext);
        gonggaotext = findViewById(R.id.gonggaotext);
        activtylist = findViewById(R.id.activtylist);
        activityimg = findViewById(R.id.activityimg);
        gonggao = findViewById(R.id.gonggao);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        activtylist.setLayoutManager(layoutManager);
        adapter = new Adapter();
        activtylist.setAdapter(adapter);
        getactivitydata();
        flag = 1;
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        huodongtext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gonggaotext.setTextColor(getResources().getColor(R.color.moccasin));
                huodongimgbt.setImageResource(R.mipmap.huodongup);
                huodongtext.setTextColor(getResources().getColor(R.color.blueviolet));
                gonggaoimgbt.setImageResource(R.mipmap.gonggaobt);
                getactivitydata();
                gonggao.setVisibility(View.GONE);
                activityimg.setVisibility(View.VISIBLE);
                notices.clear();
                positions = 0;
                adapter.notifyDataSetChanged();
                flag = 1;
            }
        });
        gonggaotext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                huodongtext.setTextColor(getResources().getColor(R.color.moccasin));
                gonggaoimgbt.setImageResource(R.mipmap.gonggaoup);
                gonggaotext.setTextColor(getResources().getColor(R.color.blueviolet));
                huodongimgbt.setImageResource(R.mipmap.huodongbt);
                gonggaogetdata();
                gonggao.setVisibility(View.VISIBLE);
                activityimg.setVisibility(View.GONE);
                flag = 2;
                positions = 0;
                activityBeans.clear();
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void gonggaogetdata() {
        HttpJsonUtils.getHttpApiInstance()
                .getbanner(Constants.getuseAdress() + "api/m/lottery/getHomeLotteryDataNew")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResBanner>() {
                    @Override
                    protected void onCustomNext(ResBanner resBanner) {
                        SYSOUtils.sOut_("首页banner" + resBanner.data.notices);
                        if (resBanner.code == 0) {
                            notices.clear();
                            notices.addAll(resBanner.data.notices);
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    private void getactivitydata() {
        HttpJsonUtils.getHttpApiInstance()
                .getactivitydata(Constants.getuseAdress() + "api/m/lottery/getQPData")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResActivity>() {
                    @Override
                    protected void onCustomNext(ResActivity resActivity) {
                        if (resActivity.code == 0 && resActivity.data.qp_activity.size() != 0) {
                            activityBeans.clear();
                            activityBeans.addAll(resActivity.data.qp_activity);
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });

    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(HuodongActivity.this)
                    .inflate(R.layout.textitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
//            holder.textitem.setBackgroundResource(R.mipmap.settingbt);
            if (positions == position) {
                holder.textitem.setBackgroundResource(R.mipmap.settingup);
            } else {
                holder.textitem.setBackgroundResource(R.mipmap.settingbt);
            }
            if (flag == 1) {
                holder.textitem.setText("" + activityBeans.get(position).title);
                Glide.with(HuodongActivity.this).load(activityBeans.get(0).img).into(activityimg);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        positions = position;
                        Glide.with(HuodongActivity.this).load(activityBeans.get(position).img).placeholder(R.mipmap.nodataimg).into(activityimg);
                        adapter.notifyDataSetChanged();
                    }
                });
            } else {
                holder.textitem.setText("" + notices.get(position).title);
                gonggao.setText("" + notices.get(0).content);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        positions = position;
                        gonggao.setText("" + notices.get(position).content);
                        adapter.notifyDataSetChanged();
                    }
                });
            }

        }

        @Override
        public int getItemCount() {
            if (flag == 1) {
                return activityBeans.size();
            } else {
                return notices.size();
            }

        }

        @Override
        public void onItemClick(View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView textitem;

            MViewHolder(View itemView) {
                super(itemView);
                textitem = (TextView) itemView.findViewById(R.id.textitem);
            }
        }
    }
}
